package com.simple.card.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.simple.card.Utils.AesUtil;
import com.simple.card.Utils.ResultVOUtil;
import com.simple.card.Utils.SmsUtil;
import com.simple.card.VO.ResultVO;
import com.simple.card.annotation.SignCheck;
import com.simple.card.enums.ResultEnum;
import com.simple.card.exception.CreditException;
import com.simple.card.form.SendSmsForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/sms")
@Slf4j
public class SmsController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 发送手机验证码
     * @param smsForm
     * @param bindingResult
     * @return
     * @throws ClientException
     */
    @PostMapping("/code")
    @SignCheck(sign = false)
    public ResultVO getCode(@Valid SendSmsForm smsForm,
                            BindingResult bindingResult) throws ClientException {
        if(bindingResult.hasErrors()) {
            log.error("[用户登录] 参数不正确,smsForm={}", smsForm);
            throw new CreditException(ResultEnum.PARAM_ERROR.getCode(),bindingResult.getFieldError().getDefaultMessage());
        }
        SendSmsResponse sendSmsResponse = SmsUtil.sendSms(smsForm.getPhone(), redisTemplate);
        return ResultVOUtil.success();
    }
}
